package com.cloudptt.wp.ui.views;

import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.comm.Const;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.adapter.GCallEventAdapter;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.message.GroupMemberActivity;
import com.cloudptt.wp.ui.message.SessionActivity;
import com.cloudptt.wp.ui.views.listener.CustomListener;
import com.cloudptt.wp.ui.views.listener.DialListener;
import com.mcptt.db.model.AddrInfo;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.view.View.*;


public class PTTLayout extends LinearLayout implements OnClickListener, OnTouchListener{

	public static final String TAG = "PTTLayout";
	private Context context = null;
	private DialLayout mDialLayout = null;
	private LinearLayout empty_ll;
	private LinearLayout mPttLayout;
	private ImageView mPttIv;
	private ImageView mAcceptIv;
	private ImageView mDiaIv;
	private ImageView mGroupIv;
	private ImageView mGcallVideoIv;
	private ImageView mHangupIv;
	private boolean isShowDia = false;

//	private AnimationDrawable animationDrawable;

	private GCallEventAdapter mCallEventAdapter = null;

	private CustomListener customListener;

	public PTTLayout(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	public PTTLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	public PTTLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		this(context, attrs, defStyleAttr, 0);
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	public void setDialListener(DialListener listener) {
		if(mDialLayout != null)
		{
			mDialLayout.setDialListener(listener);
		}
	}

	public PTTLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
		// TODO Auto-generated constructor stub
		this.context = context;
		Log.d(TAG, "PTTLayout new");

		LayoutInflater.from(context).inflate(R.layout.ptt_layout, this);
		mDialLayout = (DialLayout)findViewById(R.id.diaLy);
		mDialLayout.setVisibility(View.GONE);
		empty_ll = (LinearLayout)findViewById(R.id.empty_ll);
		mPttLayout = (LinearLayout)findViewById(R.id.ptt_layout);
		mDiaIv = (ImageView)findViewById(R.id.diaIv);
		mGroupIv = (ImageView)findViewById(R.id.groupIv);
//		mWarningImageView = (ImageView)findViewById(R.id.warning_imageview);
		mPttIv = (ImageView)findViewById(R.id.pttIv);
		mGcallVideoIv = (ImageView)findViewById(R.id.gcallVideoIv);
		mAcceptIv = (ImageView)findViewById(R.id.acceptIv);
		mHangupIv = (ImageView)findViewById(R.id.hangupIv);
		mHangupIv.setVisibility(View.GONE);
	}

	public void setCallAdapter(GCallEventAdapter callEventAdapter){
		Log.d(TAG, "setCallAdapter");
		mCallEventAdapter = callEventAdapter;

		onResume();
	}

	private void updatePttUI(){
		if(mPttIv != null){
			//mPttImageView.setPressed(true);
			mPttIv.setImageResource(R.drawable.icon_group_ptt_pressed);
		}

//		if(mPttImageView != null){
//			animationDrawable = (AnimationDrawable)mPttImageView.getDrawable();
//			animationDrawable.start();
//		}
		//				showCallStatusTv(true);
		String numStr = mDialLayout.getNumStr();
		String nameStr = numStr;
		boolean isEmergency = false;
		if(numStr != null
				&& numStr.length() >0)
		{
			if(numStr.length() >30)
			{
				CommDlg.showToast(context,R.string.err_num_toolong);
				return;
			}
			if(numStr.equals("299"))
			{
				isEmergency = true;
			}
			if(numStr.length() == 3)
			{
				numStr = "50" + numStr;
				nameStr = nameStr;
			}
			SysApplication app = ((SysApplication) context.getApplicationContext());
			Product product = app.getProduct();
			AddrInfo groupInfo = new AddrInfo(AddrInfo.GroupType_Crsc,nameStr,numStr,0);
			product.setCurGroupInfo(groupInfo);
		}
		if(mCallEventAdapter != null){
			mCallEventAdapter.onPtt(Const.MSG_TALK_ON, true,false,true,isEmergency);
		}

	}
	public void onStop()
	{
		Log.d(TAG, "onStop");
		mDiaIv.setOnClickListener(null);
		mGcallVideoIv.setOnClickListener(null);
		mGroupIv.setOnClickListener(null);
		mPttLayout.setOnTouchListener(null);
		mPttIv.setOnTouchListener(null);
		mHangupIv.setOnClickListener(null);
		mAcceptIv.setOnClickListener(null);
	}
	public void onResume()
	{
		Log.d(TAG, "onResume");
		mDiaIv.setOnClickListener(this);
		mGcallVideoIv.setOnClickListener(this);
		mGroupIv.setOnClickListener(this);
		mPttLayout.setOnTouchListener(this);
		mPttIv.setOnTouchListener(this);
		mHangupIv.setOnClickListener(this);
		mAcceptIv.setOnClickListener(this);
	}

	private void doPttOff(boolean isMust){
		Log.d(TAG, "doPttOff");
//		if(animationDrawable != null){
//			animationDrawable.stop();
//		}
		if(mPttIv != null){
			mPttIv.setPressed(false);
			mPttIv.setImageResource(R.drawable.icon_group_ptt);
		}
		//				showCallStatusTv(false);
		if(mCallEventAdapter != null){
			mCallEventAdapter.onPtt(Const.MSG_TALK_OFF, true,false,true,false);
		}
	}

	private void pressPTT(boolean usingPtt){
		if(usingPtt){
			updatePttUI();
		}
	}

	public void setCustomView(int resId, CustomListener listener){
		mDiaIv.setImageResource(resId);
		customListener = listener;
	}

	public void showAcceptBtn()
	{
		mAcceptIv.setVisibility(View.VISIBLE);
	}

	public void showCallBtn(boolean isShow){
		Log.d(TAG, "showCallBtn isShow = " + isShow);
		if(isShow){
			if(mHangupIv != null
					&& !mHangupIv.isShown()){
				if(mCallEventAdapter.isEmergencyMt())
				{
					mHangupIv.setVisibility(View.GONE);
				}
				else
				{
					mHangupIv.setVisibility(View.VISIBLE);
				}
			}
			if(mGroupIv != null
					&& !mGroupIv.isShown()){
				mGroupIv.setVisibility(View.VISIBLE);
			}
			if(mDiaIv != null
					&& mDiaIv.isShown()){
				mDiaIv.setVisibility(View.GONE);
			}
			if(mGcallVideoIv != null
					&& mGcallVideoIv.isShown()){
				mGcallVideoIv.setVisibility(View.GONE);
			}
		}else{
			if(mHangupIv != null
					&& mHangupIv.isShown()){
				mHangupIv.setVisibility(View.GONE);
			}
			if(mGroupIv != null
					&& mGroupIv.isShown()){
				mGroupIv.setVisibility(View.GONE);
			}
			if(mDiaIv != null
					&& !mDiaIv.isShown()){
				mDiaIv.setVisibility(View.VISIBLE);
			}
			if(mGcallVideoIv != null
					&& !mGcallVideoIv.isShown()){
				mGcallVideoIv.setVisibility(View.VISIBLE);
			}
		}
	}

	public void clearState(){
		Log.d(TAG, "clearState");
		showCallBtn(false);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if(R.id.hangupIv == view.getId()){
			mAcceptIv.setVisibility(View.GONE);
			if(mCallEventAdapter != null){
				mCallEventAdapter.hangUpGroupCall();
			}
		}
//		else if(R.id.acceptIv == view.getId()){
//			mAcceptIv.setVisibility(View.GONE);
//			if(mCallEventAdapter != null){
//				mCallEventAdapter.acceptGroupCall();
//			}
//		}
		else if(R.id.gcallVideoIv == view.getId()){
			if(mCallEventAdapter != null){
				mCallEventAdapter.onPtt(Const.MSG_TALK_ON, true,true,true,false);
			}
		}
		else if(R.id.diaIv == view.getId()){
			Log.d(TAG, "click custom");
			isShowDia = !isShowDia;
			mDialLayout.clear();
			if(isShowDia) {
				empty_ll.setBackgroundColor(Color.WHITE);
				mDiaIv.setImageResource(R.drawable.btn_dia_pressed);
				mDialLayout.setVisibility(View.VISIBLE);
			}
			else {
				empty_ll.setBackgroundColor(Color.parseColor("#DAE4EA"));
				mDiaIv.setImageResource(R.drawable.btn_dia_normal);
				mDialLayout.clear();
				mDialLayout.setVisibility(View.GONE);
			}
			if(customListener != null)
			{
				customListener.customFunc();
			}
		}
		else if(R.id.groupIv == view.getId()){
			AddrInfo peerAddrInfo = mCallEventAdapter.getCurGroupInfo();
			if(peerAddrInfo != null) {
				Intent intent = new Intent();
				intent.setClass(context, GroupMemberActivity.class);
				intent.getBooleanExtra(GroupMemberActivity.KEY_SHOW, true);
				intent.putExtra(SessionActivity.KEY, peerAddrInfo);
				context.startActivity(intent);
			}
		}

	}

	@Override
	public boolean onTouch(View view, MotionEvent event) {
		// TODO Auto-generated method stub
		Log.d(TAG, "event action = "+event.getAction());
		if(view.getId() == R.id.ptt_layout
				|| view.getId() == R.id.pttIv){
			if(event.getAction() == MotionEvent.ACTION_DOWN){
				pressPTT(true);
				return true;
			}else if(event.getAction() == MotionEvent.ACTION_UP){
				doPttOff(false);
				return true;
			}
		}
		return false;
	}

}
